<template>
    <div class="app-container">
        <div class="search-container">
            <el-form v-show="showSearch" ref="queryParamsRef" :inline="true" :model="queryParams" label-width="100px">
                <el-form-item label="检验申请主表ID" prop="applyId">
                    <el-input v-model="queryParams.applyId" clearable placeholder="请输入检验申请主表ID"
                        @keyup.enter="handleQuery" />
                </el-form-item>
                <el-form-item label="诊疗项目ID" prop="clinicItemId">
                    <el-input v-model="queryParams.clinicItemId" clearable placeholder="请输入诊疗项目ID"
                        @keyup.enter="handleQuery" />
                </el-form-item>
                <el-form-item label="检验项目名称" prop="itemName">
                    <el-input v-model="queryParams.itemName" clearable placeholder="请输入检验项目名称"
                        @keyup.enter="handleQuery" />
                </el-form-item>
                <el-form-item label="检验类别" prop="itemType">
                    <el-select v-model="queryParams.itemType" clearable placeholder="请选择检验类别">
                        <el-option v-for="dict in dictDataMap['${dictType}']" :key="dict.dictValue"
                            :label="dict.dictLabel" :value="dict.dictValue" />
                    </el-select>
                </el-form-item>
                <el-form-item label="检验项目code" prop="itemCode">
                    <el-input v-model="queryParams.itemCode" clearable placeholder="请输入检验项目code"
                        @keyup.enter="handleQuery" />
                </el-form-item>
                <el-form-item label="费用" prop="costs">
                    <el-input v-model="queryParams.costs" clearable placeholder="请输入费用" @keyup.enter="handleQuery" />
                </el-form-item>
                <el-form-item label="机构ID" prop="orgId">
                    <el-input v-model="queryParams.orgId" clearable placeholder="请输入机构ID" @keyup.enter="handleQuery" />
                </el-form-item>
                <el-form-item label="备注信息" prop="remarks">
                    <el-input v-model="queryParams.remarks" clearable placeholder="请输入备注信息"
                        @keyup.enter="handleQuery" />
                </el-form-item>
                <el-form-item label="真实开单时间" prop="realDate">
                    <el-date-picker v-model="queryParams.realDate" clearable placeholder="请选择真实开单时间" type="date"
                        value-format="YYYY-MM-DD">
                    </el-date-picker>
                </el-form-item>
                <el-form-item>
                    <query-form-button @handleQuery="handleQuery" @resetQuery="resetQuery" />
                </el-form-item>
            </el-form>
        </div>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button v-hasPerm="['business:labItem:add']" plain type="primary" @click="handleAdd">
                    <template #icon>
                        <i-ep-plus />
                    </template>
                    新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button v-hasPerm="['business:labItem:edit']" :disabled="single" plain type="success"
                    @click="handleUpdate">
                    <template #icon>
                        <i-ep-edit />
                    </template>
                    修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button v-hasPerm="['business:labItem:remove']" :disabled="multiple" plain type="danger"
                    @click="handleDelete">
                    <template #icon>
                        <i-ep-delete />
                    </template>
                    删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button v-hasPerm="['business:labItem:export']" plain type="warning" @click="handleExport">
                    <template #icon>
                        <i-ep-download />
                    </template>
                    导出
                </el-button>
            </el-col>
        </el-row>

        <el-table v-adaptive :border="true" :data="labItemList" :header-cell-style="$headerCellStyle" :stripe="true"
            highlight-current-row @selection-change="handleSelectionChange">
            <el-table-column :align="'center'" type="selection" width="55" />
            <el-table-column v-if="true" :align="'center'" label="主键" prop="id" />
            <el-table-column :align="'center'" label="检验申请主表ID" prop="applyId" />
            <el-table-column :align="'center'" label="诊疗项目ID" prop="clinicItemId" />
            <el-table-column :align="'center'" label="检验项目名称" prop="itemName" />
            <el-table-column :align="'center'" label="检验类别" prop="itemType" />
            <el-table-column :align="'center'" label="检验项目code" prop="itemCode" />
            <el-table-column :align="'center'" label="费用" prop="costs" />
            <el-table-column :align="'center'" label="机构ID" prop="orgId" />
            <el-table-column :align="'center'" label="备注信息" prop="remarks" />
            <el-table-column :align="'center'" label="真实开单时间" prop="realDate" width="180">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.realDate, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column :align="'center'" class-name="small-padding fixed-width" label="操作" width="220">
                <template #default="scope">
                    <el-button v-hasPerm="['business:labItem:edit']" type="success" @click="handleUpdate(scope.row)">
                        <template #icon>
                            <i-ep-edit />
                        </template>
                        修改
                    </el-button>
                    <el-button v-hasPerm="['business:labItem:remove']" type="danger" @click="handleDelete(scope.row)">
                        <template #icon>
                            <i-ep-delete />
                        </template>
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum"
            :total="total" @pagination="getList" />

        <!-- 添加或修改检验申请子表对话框 -->
        <el-dialog v-model="open" :close-on-click-modal="false" :title="title" append-to-body width="500px">
            <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="检验申请主表ID" prop="applyId">
                    <el-input v-model="form.applyId" placeholder="请输入检验申请主表ID" />
                </el-form-item>
                <el-form-item label="诊疗项目ID" prop="clinicItemId">
                    <el-input v-model="form.clinicItemId" placeholder="请输入诊疗项目ID" />
                </el-form-item>
                <el-form-item label="检验项目名称" prop="itemName">
                    <el-input v-model="form.itemName" placeholder="请输入检验项目名称" />
                </el-form-item>
                <el-form-item label="检验类别" prop="itemType">
                    <el-select v-model="form.itemType" placeholder="请选择检验类别">
                        <el-option v-for="dict in dictDataMap['${dictType}']" :key="dict.dictValue"
                            :label="dict.dictLabel" :value="dict.dictValue"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="检验项目code" prop="itemCode">
                    <el-input v-model="form.itemCode" placeholder="请输入检验项目code" />
                </el-form-item>
                <el-form-item label="费用" prop="costs">
                    <el-input v-model="form.costs" placeholder="请输入费用" />
                </el-form-item>
                <el-form-item label="机构ID" prop="orgId">
                    <el-input v-model="form.orgId" placeholder="请输入机构ID" />
                </el-form-item>
                <el-form-item label="备注信息" prop="remarks">
                    <el-input v-model="form.remarks" placeholder="请输入备注信息" />
                </el-form-item>
                <el-form-item label="真实开单时间" prop="realDate">
                    <el-date-picker v-model="form.realDate" clearable placeholder="请选择真实开单时间" type="datetime"
                        value-format="YYYY-MM-DD HH:mm:ss">
                    </el-date-picker>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>
<script lang="ts">
export default {
    name: "LabItem"
}
</script>

<script lang="ts" setup>
import { addLabItem, delLabItem, getLabItem, listLabItem, updateLabItem } from "@/api/patient/labItem";

export interface LabItem extends PageQuery {
    applyId: string,
    clinicItemId: string,
    itemName: string,
    itemType: string,
    itemCode: string,
    costs: number,
    orgId: string,
    remarks: string,
    realDate: string
}

const { proxy } = getCurrentInstance() as any;

const formRef = ref();
const queryParamsRef = ref()
const labItemList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = ref({
    form: {
        applyId: "",
        clinicItemId: "",
        itemName: "",
        itemType: "",
        itemCode: "",
        costs: 0,
        orgId: "",
        remarks: "",
        realDate: ""
    },
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        applyId: "",
        clinicItemId: "",
        itemName: "",
        itemType: "",
        itemCode: "",
        costs: 0,
        orgId: "",
        remarks: "",
        realDate: ""
    },
    rules: {
        id: [
            {
                required: true, message: "主键不能为空", trigger: "blur"
            }
        ],
        applyId: [
            {
                required: true, message: "检验申请主表ID不能为空", trigger: "blur"
            }
        ],
        clinicItemId: [
            {
                required: true, message: "诊疗项目ID不能为空", trigger: "blur"
            }
        ],
        itemName: [
            {
                required: true, message: "检验项目名称不能为空", trigger: "blur"
            }
        ],
        itemCode: [
            {
                required: true, message: "检验项目code不能为空", trigger: "blur"
            }
        ],
        costs: [
            {
                required: true, message: "费用不能为空", trigger: "blur"
            }
        ],
        orgId: [
            {
                required: true, message: "机构ID不能为空", trigger: "blur"
            }
        ],
    }
});

const { queryParams, form, rules } = toRefs(data.value);

/** 查询检验申请子表列表 */
function getList() {
    listLabItem(queryParams.value).then((response: any) => {
        labItemList.value = response.rows;
        total.value = response.total;
    });
}

// 取消按钮
function cancel() {
    open.value = false;
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    queryParamsRef.value.resetFields()
    // proxy.resetForm("queryRef");
    handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
    if (!queryParams.value.orgId) {
        return ElMessage.error("请先选择机构")
    }
    open.value = true;
    title.value = "添加检验申请子表";
}

/** 修改按钮操作 */
function handleUpdate(row) {
    open.value = true;
    const _id = row.id || ids.value
    getLabItem(_id).then((response: any) => {
        form.value = response.data;
        title.value = "修改检验申请子表";
    });
}

/** 提交按钮 */
function submitForm() {
    (formRef.value as any).validate(valid => {
        if (valid) {
            buttonLoading.value = true;
            if (form.value.id != null) {
                updateLabItem(form.value).then((response: any) => {
                    ElMessage.success("修改成功");
                    open.value = false;
                    getList();
                }).finally(() => {
                    buttonLoading.value = false;
                });
            } else {
                addLabItem(form.value).then((response: any) => {
                    ElMessage.success("新增成功");
                    open.value = false;
                    getList();
                }).finally(() => {
                    buttonLoading.value = false;
                });
            }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const _ids = row.id ||
        ids.value;
    ElMessageBox.confirm('是否确认删除检验申请子表编号为"' + ids + '"的数据项？',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => {
        delLabItem(_ids).then(() => {
            getList();
            ElMessage.success("删除成功");
        })
    }).catch(() => {
    })
}


/** 导出按钮操作 */
function handleExport() {
    proxy.download('business/labItem/export', {
        ...queryParams.value
    }, `labItem_${new Date().getTime()}.xlsx`)
}

getList();
</script>
